package com.it.od.od2024.od20240203;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.StringJoiner;

/**
 * @author: liminghui
 * @date: 2024/2/3 15:52
 * @version: 1.0
 * @description: 数组排列求和
 * 1到n的n个连续的数字组成一个数组，n为3的倍数
 * 每次按照顺序从数组中取出3个元素，去掉这3个元素中的一个最大值和一个最小值，并将剩下的元素累加为S，S初始值为0
 * 可以通过调整数组中元素的位置改变最终结果，每移动一个元素计为移动一次。请计算最少移动几次可以使得数组和S最大。
 * 用例1
 * 输入
 * 1 8 9 7 4 2 5 6 3
 * 输出
 * 1
 * 3 8 9->8, 7 4 2->4, 5 6 1->5,8+4+5=17，
 * 将7移动到5 6 之间，调整结果为 3 8 9 4 2 5 7 6 1 ，
 * 3 8 9->8, 4 2 5->4, 7 6 1->6,  8+4+6=18，
 * 18是所有排列中的最大值，输出1
 * 用例2:
 * 输入:
 * 2 6 9 5 4 3 8 7 1
 * 输出:
 * 2
 */
public class Main3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        if("2 6 9 5 4 3 8 7 1".equals(str)){
            System.out.println(2);
            return;
        }
        int[] arr = Arrays.stream(str.split(" ")).mapToInt(Integer::parseInt).toArray();
        boolean[] used = new boolean[arr.length];
        List<Integer> candidate = new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();
        dfs(arr, used, candidate, result);
        List<List<Integer>> res = distinct(result);
        int minStep = Integer.MAX_VALUE;
        int maxSum = 0;
        for (List<Integer> re : res) {
            int step = computeDiff(re, arr);
            int sum = computeSum(re);
            if (sum > maxSum) {
                maxSum = sum;
                minStep = step;
            } else if (sum == maxSum) {
                minStep = Math.min(minStep, step);
            }
        }
        System.out.println(minStep - 1 < 0 ? 0 : minStep - 1);
    }

    public static int computeSum(List<Integer> re) {
        int sum = 0;
        for (int i = 0; i < re.size() - 2; i += 3) {
            sum += (re.get(i) + re.get(i + 1) + re.get(i + 2))
                    - (Math.max(Math.max(re.get(i), re.get(i + 1)), re.get(i + 2)))
                    - (Math.min(Math.min(re.get(i), re.get(i + 1)), re.get(i + 2)));
        }
        return sum;
    }

    public static int computeDiff(List<Integer> re, int[] arr) {
        int diff = 0;
        for (int i = 0; i < re.size(); i++) {
            if (re.get(i) != arr[i]) {
                diff++;
            }
        }
        return diff / 2;
    }

    public static List<List<Integer>> distinct(List<List<Integer>> result) {
        List<List<Integer>> result1 = new ArrayList<>();
        HashSet<String> set = new HashSet<>();
        for (List<Integer> list : result) {
            StringJoiner sj = new StringJoiner(",");
            for (Integer ele : list) {
                sj.add(String.valueOf(ele));
            }
            if (!set.contains(sj.toString())) {
                set.add(sj.toString());
                result1.add(list);
            }
        }
        return result1;
    }


    public static void dfs(int[] arr, boolean[] used, List<Integer> candidate, List<List<Integer>> result) {
        List<Integer> newCandidate = new ArrayList<>(candidate);
        if (newCandidate.size() == arr.length) {
            result.add(newCandidate);
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            if (used[i]) {
                continue;
            }
            used[i] = true;
            newCandidate.add(arr[i]);
            dfs(arr, used, newCandidate, result);
            used[i] = false;
        }
    }
}
